const canvas = document.getElementById('gameCanvas');
const ctx = canvas.getContext('2d');

// 游戏设置
const gridSize = 20;
const tileCount = canvas.width / gridSize;

// 蛇的初始状态
let snake = [
    {x: 10, y: 10}
];

// 食物的位置
let food = {
    x: Math.floor(Math.random() * tileCount),
    y: Math.floor(Math.random() * tileCount)
};

// 蛇的移动方向
let dx = 0;
let dy = 0;

// 游戏循环
function gameLoop() {
    clearCanvas();
    moveSnake();
    checkCollision();
    drawSnake();
    drawFood();
    setTimeout(gameLoop, 100);
}

// 清空画布
function clearCanvas() {
    ctx.fillStyle = 'white';
    ctx.fillRect(0, 0, canvas.width, canvas.height);
}

// 新增吃的状态变量
let isEating = false;

// 移动蛇
function moveSnake() {
    const head = {
        x: snake[0].x + dx,
        y: snake[0].y + dy
    };
    snake.unshift(head);

    // 检查是否吃到食物
    if (head.x === food.x && head.y === food.y) {
        // 触发吃的动作
        isEating = true;
        setTimeout(() => {
            isEating = false;
        }, 100);

        // 生成新的食物
        food = {
            x: Math.floor(Math.random() * tileCount),
            y: Math.floor(Math.random() * tileCount)
        };
    } else {
        // 移除蛇的尾部
        snake.pop();
    }
}

// 检查碰撞
function checkCollision() {
    const head = snake[0];
    // 检查是否撞到边界
    if (head.x < 0 || head.x >= tileCount || head.y < 0 || head.y >= tileCount) {
        // 游戏结束
        alert('游戏结束！');
        location.reload();
    }
    // 检查是否撞到自己
    for (let i = 1; i < snake.length; i++) {
        if (head.x === snake[i].x && head.y === snake[i].y) {
            // 游戏结束
            alert('游戏结束！');
            location.reload();
        }
    }
}

// 修改绘制蛇的函数
function drawSnake() {
    snake.forEach((segment, index) => {
        if (index === 0) {
            // 绘制蛇头
            ctx.fillStyle = 'darkgreen';
            if (isEating) {
                const centerX = (segment.x * gridSize) + gridSize / 2;
                const centerY = (segment.y * gridSize) + gridSize / 2;
                ctx.beginPath();
                if (dx === 1) {
                    // 向右移动
                    ctx.arc(centerX, centerY, gridSize / 2, 0.75 * Math.PI, 1.25 * Math.PI);
                    ctx.lineTo(centerX, centerY - gridSize / 2);
                    ctx.lineTo(centerX, centerY + gridSize / 2);
                } else if (dx === -1) {
                    // 向左移动
                    ctx.arc(centerX, centerY, gridSize / 2, 0.25 * Math.PI, 0.75 * Math.PI);
                    ctx.lineTo(centerX, centerY + gridSize / 2);
                    ctx.lineTo(centerX, centerY - gridSize / 2);
                } else if (dy === 1) {
                    // 向下移动
                    ctx.arc(centerX, centerY, gridSize / 2, 0 * Math.PI, 0.5 * Math.PI);
                    ctx.lineTo(centerX - gridSize / 2, centerY);
                    ctx.lineTo(centerX + gridSize / 2, centerY);
                } else if (dy === -1) {
                    // 向上移动
                    ctx.arc(centerX, centerY, gridSize / 2, 0.5 * Math.PI, 1 * Math.PI);
                    ctx.lineTo(centerX + gridSize / 2, centerY);
                    ctx.lineTo(centerX - gridSize / 2, centerY);
                }
                ctx.closePath();
                ctx.fill();
            } else {
                // 绘制闭嘴状态的蛇头
                ctx.fillRect(segment.x * gridSize, segment.y * gridSize, gridSize, gridSize);
            }
        } else {
            // 绘制蛇身
            ctx.fillStyle = 'green';
            ctx.fillRect(segment.x * gridSize, segment.y * gridSize, gridSize, gridSize);
        }
    });
}

// 绘制食物
function drawFood() {
    ctx.fillStyle = 'red';
    ctx.fillRect(food.x * gridSize, food.y * gridSize, gridSize, gridSize);
}

// 处理键盘输入
document.addEventListener('keydown', function(event) {
    switch (event.key) {
        case 'ArrowUp':
            if (dy === 0) {
                dx = 0;
                dy = -1;
            }
            break;
        case 'ArrowDown':
            if (dy === 0) {
                dx = 0;
                dy = 1;
            }
            break;
        case 'ArrowLeft':
            if (dx === 0) {
                dx = -1;
                dy = 0;
            }
            break;
        case 'ArrowRight':
            if (dx === 0) {
                dx = 1;
                dy = 0;
            }
            break;
    }
});

// 启动游戏
gameLoop();